package retrofitdemo.com.night.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzhoujay.richtext.RichText;

import java.util.List;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import retrofitdemo.com.night.MainActivity;
import retrofitdemo.com.night.R;
import retrofitdemo.com.night.bean.BWGClassBean;
import retrofitdemo.com.night.databinding.ItemBwgClassBinding;
import retrofitdemo.com.night.utils.GlideUtil;
import retrofitdemo.com.night.utils.OnItemClickListener;

/**
 * Created by 86178 on 2020/3/12.
 * author mogen
 * 用心，成就一切。
 */
public class BWGClassAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<BWGClassBean.DataBean> strings;
    private Context mcontext;
    OnItemClickListener mOnItemClickLitener;

    public BWGClassAdapter(Context context, List<BWGClassBean.DataBean> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemBwgClassBinding binding = DataBindingUtil.inflate( LayoutInflater.from( mcontext ),
                R.layout.item_bwg_class, parent, false );
        binding.setAdapter( this );
        return new TourViewHolder( binding );
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData( strings.get( position ) );
        TourViewHolder tv = (TourViewHolder) holder;
        GlideUtil.setRoundUriMethod( mcontext, strings.get( position ).getImgurl(), tv.ivModule, 10 );
        RichText.from( strings.get(position).getText().toString() ).bind( mcontext )
                .into( tv.tvModuleCon );

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick( holder.itemView, pos );
                }
            } );

            holder.itemView.setOnLongClickListener( new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick( holder.itemView, pos );
                    return false;
                }
            } );
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<BWGClassBean.DataBean> {
        ItemBwgClassBinding functionBinding;
        TextView tvModuleCon;
        ImageView ivModule;

        public TourViewHolder(ItemBwgClassBinding dataBinding) {
            super( dataBinding.getRoot() );
            functionBinding = dataBinding;
            tvModuleCon = dataBinding.tvModuleCon;
            ivModule = dataBinding.ivModule;
        }

        @Override
        public void bindData(BWGClassBean.DataBean data) {
            functionBinding.setBean( data );
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

}